<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级类型" prop="remarks">
        <el-select v-model="queryParams.remarks" style="width: 180px" placeholder="请选择" @keyup.enter.native="handleQuery">
          <el-option label="java" :value="java"></el-option>
          <el-option label="mysql" :value="mysql"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teaching:classes:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teaching:classes:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="班级名称" align="center" prop="className" >
        <template slot-scope="scope">
           <el-link type="primary" @click="handleSelect(scope.row)">{{scope.row.className}}</el-link>
        </template>
<!--        <template slot-scope="scope">-->
<!--          <el-link :href="scope.row.url" type="primary" target="_blank">{{scope.row.asin}}</el-link>-->
<!--        </template>-->

      </el-table-column>
      <el-table-column label="开班时间" align="center" prop="beginDate" />
      <el-table-column label="结班时间" align="center" prop="endDate"/>
      <el-table-column label="班级类型" align="center" prop="remarks" />
      <el-table-column label="是否毕业" align="center" prop="">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.restDay === 1">正常</el-tag>
          <el-tag type="primary" v-else-if="scope.row.restDay === 0">毕业</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否招生" align="center" prop=""/>
      <el-table-column label="招生链接" align="center" prop=""/>
      <el-table-column label="休息日" align="center" prop="restDay" >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.restDay === 1">周一</el-tag>
          <el-tag type="primary" v-else-if="scope.row.restDay === 2">周二</el-tag>
          <el-tag type="primary" v-else-if="scope.row.restDay === 3">周三</el-tag>
          <el-tag type="primary" v-else-if="scope.row.restDay === 4">周四</el-tag>
          <el-tag type="primary" v-else-if="scope.row.restDay === 5">周五</el-tag>
          <el-tag type="primary" v-else-if="scope.row.restDay === 6">周六</el-tag>
          <el-tag type="primary" v-else-if="scope.row.restDay === 7">周日</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="自习日" align="center" prop="selfStudyDay" >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.selfStudyDay === 1">周一</el-tag>
          <el-tag type="primary" v-else-if="scope.row.selfStudyDay === 2">周二</el-tag>
          <el-tag type="primary" v-else-if="scope.row.selfStudyDay === 3">周三</el-tag>
          <el-tag type="primary" v-else-if="scope.row.selfStudyDay === 4">周四</el-tag>
          <el-tag type="primary" v-else-if="scope.row.selfStudyDay === 5">周五</el-tag>
          <el-tag type="primary" v-else-if="scope.row.selfStudyDay === 6">周六</el-tag>
          <el-tag type="primary" v-else-if="scope.row.selfStudyDay === 7">周日</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teaching:classes:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teaching:classes:remove']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程内容" prop="className">
          <el-input v-model="form.className" placeholder="请输入课程内容" />
        </el-form-item>
        <el-form-item label="备注信息" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注信息" />
        </el-form-item>
        <el-form-item label="开班日期" prop="beginDate">
          <el-input v-model="form.beginDate" placeholder="请输入开班日期" />
        </el-form-item>
        <el-form-item label="休息日" prop="restDay">
          <el-input v-model="form.restDay" placeholder="请输入休息日" />
        </el-form-item>
        <el-form-item label="自习日" prop="selfStudyDay">
          <el-input v-model="form.selfStudyDay" placeholder="请输入自习日" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改课程对话框 -->
    <el-dialog title="详情" :visible.sync="xopen" width="70%" append-to-body>
      <el-tabs type="border-card">
        <el-tab-pane label="每月考勤表">
          <button @click="exportExcel" >导出excel</button>
          <div style="overflow: auto; height: 100%; width: 100%;">
            <table id="attendanceTable">
              <thead>
              <tr>
                <th>{{attendanceMonth.month}}</th>
                <th rowspan="3"></th>
                <th v-for="i in attendanceMonth.days">{{i}}</th>
              </tr>
              <tr>
                <td>星期</td>

                <td v-for="i in attendanceMonth.weekdays">{{i}}</td>
              </tr>
              <tr>
                <td>课表</td>

                <td v-for="i in attendanceMonth.teachingPlans">{{i}}</td>
              </tr>
              </thead>
              <thead v-for="stu in attendanceMonth.list">
              <tr >
                <td rowspan="3">{{stu.name}}</td>
                <td>早</td>
                <td v-for="att in stu.attendance[0].status" ><span :class="{green:att=='√',red:att=='-'}">{{att}}</span></td>
              </tr>
              <tr >
                <td>中</td>
                <td v-for="att in stu.attendance[1].status">{{att}}</td>
              </tr>
              <tr >
                <td>晚</td>
                <td v-for="att in stu.attendance[2].status">{{att}}</td>
              </tr>
              </thead>
            </table>
          </div>
        </el-tab-pane>
        <el-tab-pane label="成绩">
          <el-table v-loading="loading" :data="examdetailList">
            <el-table-column label="班级" align="center" prop="classesName" />
            <el-table-column label="第几阶段" align="center" prop="stage">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_stage" :value="scope.row.stage"/>
              </template>
            </el-table-column>
            <el-table-column label="学员名字" align="center" prop="student" />
            <el-table-column label="成绩" align="center" prop="remark">
              <el-table-column label="第几周" align="center" prop="week">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.ya_week" :value="scope.row.week"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="province"
                label="周一"
                width="120">
              </el-table-column>
              <el-table-column
                prop="city"
                label="市区"
                width="120">
              </el-table-column>
              <el-table-column
                prop="address"
                label="地址"
                width="300">
              </el-table-column>
              <el-table-column
                prop="zip"
                label="邮编"
                width="120">
              </el-table-column>
            </el-table-column>
            <el-table-column label="创建日期" align="center" prop="createTime" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
            <el-button @click="xopen = false">关闭</el-button>
          </span>
    </el-dialog>



<!--班级基础信息-->
    <el-dialog
      title="基础信息"
      :visible.sync="sopen"
      width="70%">

      <el-table v-loading="loading" :data="studentList" >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="学生id" align="center" prop="id" />
        <el-table-column label="用户名" align="center" prop="username" />
        <el-table-column label="名字" align="center" prop="name" />
        <el-table-column label="性别" align="center" prop="gender" >
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.gender === 1">男</el-tag>
            <el-tag type="primary" v-else-if="scope.row.gender === 0">女</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="生日" align="center" prop="birth" />
        <el-table-column label="联系号码" align="center" prop="contactNumber" />
        <el-table-column label="毕业院校" align="center" prop="graduateSchool" />
        <el-table-column label="专业" align="center" prop="majorAcademic" />
        <el-table-column label="毕业日期" align="center" prop="graduationDate" />
        <el-table-column label="学历" align="center" prop="degree" >
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.degree === 2">专科</el-tag>
            <el-tag type="primary" v-else-if="scope.row.degree === 3">本科</el-tag>
            <el-tag type="danger" v-else-if="scope.row.degree === 5">博士研究生</el-tag>
            <el-tag type="info" v-else-if="scope.row.degree === 1">中职/高中</el-tag>
            <el-tag type="warning" v-else-if="scope.row.degree === 4">硕士研究生</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="班级ID" align="center" prop="classesId" />
        <el-table-column label="班级名字" align="center" prop="classesName" />
      </el-table>
      <span slot="footer" class="dialog-footer">
    <el-button @click="sopen = false">关闭</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import { listAllClasses, getClasses, delClasses, addClasses, updateClasses } from "@/api/teaching/classes";
import {listStudentByClassName} from '@/api/teaching/student'
import {attendanceMonthList} from '@/api/teaching/attendance'
import { listExamdetailByClassId } from "@/api/teaching/examdetail";
export default {
  dicts: ['sys_stage', 'ya_week','ya_exam_type','ya_feeling'],
  name: "Classes",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      classesList: [],
      // 学生表格数据
      studentList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      sopen:false,
      xopen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: null,
        remarks: null,
        beginDate: null,
        restDay: null,
        selfStudyDay: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        className: [
          { required: true, message: "课程内容不能为空", trigger: "blur" }
        ],
        beginDate: [
          { required: true, message: "开班日期不能为空", trigger: "blur" }
        ],
        restDay: [
          { required: true, message: "休息日不能为空", trigger: "blur" }
        ],
        selfStudyDay: [
          { required: true, message: "自习日不能为空", trigger: "blur" }
        ]
      },
      attendanceMonth: {
        month:'2023-09',
        days:4,
        weekdays:['日','一','二','三'],
        teachingPlans:['休息','上课','上课','自习'],
        list:[
          {
            name: '德华',
            attendance: [
              { time: '早', status: ['√','旷课','迟到','√'] },
              { time: '中', status: ['√','旷课','-','-'] },
              { time: '晚', status: ['√','-','旷课','-'] }
            ]
          },
          {
            name: '小明',
            attendance: [
              { time: '早', status: ['√','-','迟到','-'] },
              { time: '中', status: ['√','-','迟到','-'] },
              { time: '晚', status: ['√','迟到','-','√'] }
            ]
          },
          {
            name: '小红',
            attendance: [
              { time: '早', status: ['√','-','-','-'] },
              { time: '中', status: ['√','-','√','-'] },
              { time: '晚', status: ['√','-','-','-'] }
            ]
          }
        ]
      },
    // 学生成绩详情表格数据
    examdetailList: [],
    };
  },
  created() {
    this.getList();

  },
  methods: {
    exportExcel(){
      // 获取表格元素
      const table = document.querySelector('table');

      // 将表格转换为工作簿对象
      const workbook = XLSX.utils.table_to_book(table);

      // 将工作簿对象转换为Excel文件的二进制数据
      const excelData = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });

      // 创建Blob对象
      const blob = new Blob([excelData], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });

      // 创建下载链接并点击下载
      const url = URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.download = 'table.xlsx';
      link.click();
    },
    getattendanceMonth(id){
      attendanceMonthList(id).then(res=>{
        if (res.code == 200){
          this.attendanceMonth = res.data;
          this.xopen = true;
        }

      })
    },
    getCourse(id){
      listExamdetailByClassId(id).then(response => {
        this.examdetailList = response.rows;
        this.total = response.total;
      });
    },
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      listAllClasses(this.queryParams).then(response => {
        this.classesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        className: null,
        remarks: null,
        beginDate: null,
        restDay: null,
        selfStudyDay: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClasses(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程";
      });
    },
    /** 查询按钮操作 */
    handleSelect(row) {
      // this.reset();
      console.log(row.id)
      const className = row.className
      console.log(className)
      listStudentByClassName({className})
        .then(response => {
        this.studentList = response.data;
        this.sopen = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClasses(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClasses(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.xopen = true
      this.getattendanceMonth(row.id);
      this.getCourse(row.id);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teaching/classes/export', {
        ...this.queryParams
      }, `classes_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
#attendanceTable {
  /*width: 100%;*/
  /*border-collapse: collapse;*/
  border-spacing: 0;
  border: 1px solid black;
}
#attendanceTable td {
  text-align: left;
  padding: 8px;
  border: 1px solid black;
}
#attendanceTable th {
  text-align: left;
  padding: 8px;
  width: 200px;
  border: 1px solid black;
  background-color: #f2f2f2;
}
.green {
  color: green;
}

.red {
  color: red;
}
</style>
